home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_362 / puzz / source / loadpic.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  252 lines

  1. /*    ILBM picture reader etc. for PUZZ    M.J.Round    January 1990
  2.     Largely copied from 'view.c' by D. John Hodgson.
  3. */
  4.  
  5. #define SR(a,b,c) if (Read(a,(char *)b,c)==-1L) return(NULL);
  6.  
  7. #define MAXWIDTH  376 /*    max non-HIRES width                    */
  8. #define MAXHEIGHT 242 /*    max non-interlaced height (NTSC)    */
  9. #define MAXCOLORS 32  /*    max # colors supported              */
  10.  
  11. #define MakeID(a,b,c,d) ((a)<<24L | (b)<<16L | (c)<<8 | (d))
  12.  
  13. #define ID_FORM MakeID('F','O','R','M')
  14. #define ID_ILBM MakeID('I','L','B','M')
  15. #define ID_BMHD MakeID('B','M','H','D')
  16. #define ID_CAMG MakeID('C','A','M','G')
  17. #define ID_CMAP MakeID('C','M','A','P')
  18. #define ID_BODY MakeID('B','O','D','Y')
  19.  
  20. typedef struct
  21.     {
  22.     long ckID,ckSize;
  23.     } Chunk;
  24.  
  25. Chunk header;
  26. char *bufstart;
  27. struct BitMap b;
  28. struct RastPort rp;
  29. struct RastPort dbuffrastport;
  30. struct BitMap dbuffbitmap;
  31.  
  32. extern struct IntuitionBase *IntuitionBase;
  33. extern struct GfxBase *GfxBase;
  34. extern BitMapHeader bmhd;
  35. extern struct TextAttr MyFont;
  36.  
  37. struct Screen *ReadILBM(BPTR fp)
  38.     {
  39.     struct NewScreen NewScreen;
  40.     struct Screen *screen;
  41.     char colormap[MAXCOLORS][3],*sourcebuf;
  42.     short colorcount;
  43.     long id,ViewModes=0;
  44.  
  45.     SR(fp,&header,(long)sizeof(header));
  46.     if (header.ckID!=ID_FORM) return(NULL);
  47.  
  48.     SR(fp,&id,(long)sizeof(id));
  49.     if (id!=ID_ILBM) return(NULL);
  50.  
  51.     for (;;)
  52.         {
  53.         SR(fp,&header,(long)sizeof(header));
  54.  
  55.         if (header.ckID==ID_BODY) break;
  56.  
  57.         switch(header.ckID)
  58.             {
  59.             case ID_BMHD:
  60.                 SR(fp,&bmhd,(long)sizeof(bmhd));
  61.                 break;
  62.  
  63.             case ID_CMAP:
  64.                 SR(fp,&colormap[0][0],(long)header.ckSize);
  65.                 colorcount=header.ckSize/3;
  66.                 break;
  67.  
  68.             case ID_CAMG:
  69.                 SR(fp,&ViewModes,(long)header.ckSize);
  70.                 break;
  71.  
  72.             default:
  73.                 Seek(fp,(((header.ckSize)+1)&(~1L)),OFFSET_CURRENT);
  74.       
  75.             }
  76.         }
  77.  
  78.     /*    Read planes into RAM for ease of decompression    */
  79.      
  80.     sourcebuf=bufstart=AllocMem((long)header.ckSize,MEMF_PUBLIC);
  81.     if (sourcebuf==0L) return (NULL);
  82.  
  83.     SR(fp,sourcebuf,(long)header.ckSize); 
  84.  
  85.     NewScreen.LeftEdge=0; NewScreen.TopEdge=0;
  86.     NewScreen.Width=bmhd.w; NewScreen.Height=bmhd.h;
  87.     NewScreen.Depth=bmhd.nPlanes;
  88.  
  89.     /*    make some forced assumptions if CAMG chunk unavailable    */
  90.  
  91.     if (!(NewScreen.ViewModes=ViewModes))
  92.         {
  93.         if (bmhd.w>MAXWIDTH) NewScreen.ViewModes|=HIRES;
  94.         if (bmhd.h>MAXHEIGHT) NewScreen.ViewModes|=LACE;
  95.         }
  96.  
  97.     NewScreen.Type=CUSTOMSCREEN;
  98.     NewScreen.Font=&MyFont;
  99.     NewScreen.Gadgets=0L;
  100.  
  101.     screen=OpenScreen(&NewScreen);
  102.  
  103.     while (colorcount--)
  104.         SetRGB4
  105.             (
  106.             &screen->ViewPort,
  107.             (long)colorcount,
  108.             colormap[colorcount][0]>>4L,colormap[colorcount][1]>>4L,
  109.             colormap[colorcount][2]>>4L
  110.             );
  111.   
  112.     return(screen);   
  113.     }       
  114.  
  115. void Expand(screen,bmhd) /* Fast line decompress/deinterleave    */
  116. struct Screen *screen;
  117. BitMapHeader *bmhd;
  118.     {
  119.     register char *sourcebuf;
  120.     register char n,*destbuf; /*    in order of preferred allocation    */
  121.     register short plane,linelen,rowbytes,i;
  122.  
  123.     sourcebuf = bufstart;
  124.     linelen=bmhd->w/8;
  125.  
  126.     for (i=0;i<bmhd->h;i++) /*    process n lines/screen    */
  127.         {
  128.         for (plane=0;plane<bmhd->nPlanes;plane++)
  129.             {    /*    process n planes/line    */
  130.             destbuf=(char *)(screen->BitMap.Planes[plane])+linelen*i;
  131.  
  132.             if (bmhd->compression== 1)
  133.                 {    /*    compressed screen?    */
  134.                 rowbytes=linelen;
  135.  
  136.                 while (rowbytes)
  137.                     {    /*    unpack until 1 scan-line complete        */
  138.                     n=*sourcebuf++;     /*    fetch block run marker    */
  139.  
  140.                     /*    uncompressed block? copy n bytes verbatim    */
  141.                     if (n>=0)
  142.                         {
  143.                         movmem
  144.                             (
  145.                             sourcebuf,
  146.                             destbuf,
  147.                             (unsigned int)++n
  148.                             );
  149.                         rowbytes-=n;
  150.                         destbuf+=n;
  151.                         sourcebuf+=n;
  152.                         }
  153.                     else
  154.                         {    /*    compr. block? expand n duplicate bytes    */
  155.                         n=-n+1;
  156.                         rowbytes-=n;
  157.                         setmem
  158.                             (
  159.                             destbuf,
  160.                             (unsigned int)n,
  161.                             (unsigned int)*sourcebuf++
  162.                             );
  163.                         destbuf+=n;
  164.                         }
  165.  
  166.                     }    /*    finish unpacking line    */
  167.                 }
  168.             else
  169.                 {    /*    uncompressed? just copy */
  170.                 movmem(sourcebuf,destbuf,(unsigned int)linelen);
  171.                 sourcebuf+=linelen;
  172.                 }
  173.             }    /*    finish interleaved planes, lines    */
  174.         }
  175.     }
  176.  
  177. struct RastPort *getbuffs(int width,int height)
  178.     {
  179.     int i;
  180.     
  181.     InitBitMap(&b,bmhd.nPlanes,bmhd.w,11);
  182.     
  183.     for (i=0; i<bmhd.nPlanes; i++)
  184.         {
  185.         b.Planes[i] = (PLANEPTR)AllocRaster(bmhd.w,11);
  186.         if(b.Planes[i] == NULL)
  187.             {
  188.             while (--i >= 0)
  189.                 FreeRaster(b.Planes[i],bmhd.w,11);
  190.  
  191.             return (NULL);
  192.             }
  193.         }
  194.  
  195.     InitRastPort(&rp);
  196.     rp.BitMap = &b;
  197.  
  198.     InitBitMap(&dbuffbitmap,bmhd.nPlanes,width,height);
  199.     
  200.     for (i=0; i<bmhd.nPlanes; i++)
  201.         {
  202.         dbuffbitmap.Planes[i] = (PLANEPTR)AllocRaster(width,height);
  203.         if(dbuffbitmap.Planes[i] == NULL)
  204.             {
  205.             while (--i >= 0)
  206.                 FreeRaster(dbuffbitmap.Planes[i],width,height);
  207.  
  208.             return (NULL);
  209.             }
  210.         }
  211.  
  212.     InitRastPort(&dbuffrastport);
  213.     dbuffrastport.BitMap = &dbuffbitmap;
  214.     
  215.     return (&dbuffrastport);
  216.     }
  217.     
  218. void saveundermenu(struct RastPort *rast)
  219.     {
  220.     ClipBlit
  221.         (
  222.         rast,0,0        /*    source posn    */
  223.         ,&rp,0,0        /*    dest posn    */
  224.         ,bmhd.w,11      /*    size        */
  225.         ,0xc0            /*    direct copy    */
  226.         );
  227.     }
  228.     
  229. void drawovermenu(struct RastPort *rast)
  230.     {
  231.     ClipBlit
  232.         (
  233.         &rp,0,0         /*    source posn */
  234.         ,rast,0,0        /*    dest posn    */
  235.         ,bmhd.w,11      /*    size        */
  236.         ,0xc0            /*    direct copy    */
  237.         );
  238.     }
  239.  
  240. void freebuffs(int width,int height)
  241.     {
  242.     int i;
  243.     
  244.     for (i=0; i<bmhd.nPlanes; i++)
  245.         FreeRaster(dbuffbitmap.Planes[i],width,height);
  246.         
  247.     for (i=0; i<bmhd.nPlanes; i++)
  248.         FreeRaster(b.Planes[i],bmhd.w,11);
  249.  
  250.     FreeMem(bufstart,(long)header.ckSize); /*    free compressed buffer    */
  251.     }
  252.